package com.xju.web;

import com.xju.entity.User;
import com.xju.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping
    public String index() {
        return "user/index";
    }

    @RequestMapping("show")
    public String show(Map model) {
        List<User> users = userService.findAll();
        if(users == null) {
            System.out.println("users is null");
        }
        model.put("users", users);
        model.put("msg", "hello");
        return "user/show" ;
    }

    @RequestMapping("addForm")
    public String showAddView() {
       return "user/addForm";
    }

    @RequestMapping("add")
    public  String add(User user, Model model) {
       userService.save(user);

       return "redirect:/user/show";
    }
}
